﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace md5Tools
{
    public class OperIni
    {
        /// <summary>
        /// 写操作
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">建</param>
        /// <param name="value">值</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
        /// <summary>
        /// 读操作
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">建</param>
        /// <param name="defValue">未读取到的默认值</param>
        /// <param name="retvalue">读取到的值</param>
        /// <param name="size">大小</param>
        /// <param name="filepath">文件路径</param>
        /// <returns>返回字符串长度</returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defValue, StringBuilder retvalue, int size, string filepath);

        [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
        private static extern int GetPrivateProfileString(string ApplicationName, string KeyName, string DefaultString, [MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer, int nSize, string FileName);

        /// <summary>
        /// 读取ini文件
        /// </summary>
        /// <param name="filepath">文件文件路径</param>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="retValue">获取的值/默认值</param>
        /// <param name="defValue">未读取到值时的默认值</param>
        /// <returns>retValue 字符的长度</returns>
        public static int ReadIni(string filepath, string section, string key, out String retValue, string defValue = "")
        {
            StringBuilder retvalue = new StringBuilder();
            retvalue.Capacity = 65535;
            retvalue.Length = 65535;
            int readByteLength = GetPrivateProfileString(section, key, defValue, retvalue, 65535, filepath);
            retValue = retvalue.ToString();
            return readByteLength;
        }
        /// <summary>
        /// 写入ini文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="section">节</param>
        /// <param name="key">建</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public static long WriteIni(string filePath, string section, string key, string value)
        {
            return WritePrivateProfileString(section, key, value, filePath);
        }
        /// <summary>
        /// 删除节
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <param name="section">节</param>
        /// <returns></returns>
        public static long DeleteSection(string filepath, string section)
        {
            return WritePrivateProfileString(section, null, null, filepath);
        }
        /// <summary>
        /// 删除键
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static long DeleteKey(string filepath, string section, string key)
        {
            return WritePrivateProfileString(section, key, null, filepath);
        }
    }
}
